package com.guigu.hkw.controller;

import com.guigu.hkw.Service.UserService;
import com.guigu.hkw.Users;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("user")
public class USerLonginController {

    @Autowired
    UserService userService;

    @RequestMapping("long")
    public String urlLOgin(Users users){
        System.out.println(users);
        try{
            //Session session= subject.getSubject(); 这是权限自带的方法和servlet的Session没有任何关系

            Subject subject = SecurityUtils.getSubject();
          //  Session session = subject.getSession();
           //  session.setTimeout(1000);  获取session的时间
            //session.setAttribute(""  ,"")  传值
            UsernamePasswordToken token=new UsernamePasswordToken(users.getUserName(),users.getUserPwd());
            System.out.println(token);
            subject.login(token);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("账号或者密码错误");
            return "/long.jsp";
        }
        System.out.println("登录成功");
        return "/home.jsp";
    }
    @GetMapping("/text1")
    public String test1(){
            userService.text1();
        return "/home.jsp";
    }
    @GetMapping("/text2")
    public String test2(){
        int i= 1/0;
            userService.text2();
        return "/home.jsp";
    }
    @GetMapping("/del")
    public String del(){
            userService.text3();
        return "/home.jsp";
    }
    @GetMapping("/find")
    public String find(){
            userService.text4();
        return "/home.jsp";
    }
}
